import { Body, Controller, Post, Get, Query } from '@nestjs/common';
import { MissionService } from './mission.service';
import { ApiBearerAuth, ApiBody, ApiExcludeEndpoint, ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreateMissionDto, MissionRecordDto, SearchMissionDto } from './mission.dto';
import { MissionEntity } from './mission.entity';
import { Param, Delete, Put } from '@nestjs/common';
import { AuthUser } from '../auth/decorators/auth-user.decorator';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { ApiTenantHeader } from '~/common/decorators/api-header-tenant.decorator';
import { PagerDto } from '~/common/dto/pager.dto';
import { StandardPagination } from '~/helper/paginate';

@ApiTags(ModuleSwaggerTags.MISSION)
@ApiBearerAuth()
@Controller()
export class MissionController {
  constructor(private readonly missionService: MissionService) {}

  @Get('page')
  @ApiOperation({ summary: '分页查询任务' })
  async getMissions(@Query() query: SearchMissionDto): Promise<StandardPagination<MissionEntity>> {
    return await this.missionService.findAll(query);
  }

  @Post()
  @ApiOperation({ summary: '创建任务' })
  @ApiBody({ type: CreateMissionDto })
  async createMission(@Body() dto: CreateMissionDto): Promise<MissionEntity> {
    return await this.missionService.create(dto);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新任务' })
  @ApiBody({ type: CreateMissionDto })
  async updateMission(
    @Param('id') id: string,
    @Body() dto: CreateMissionDto
  ): Promise<MissionEntity> {
    return await this.missionService.update(Number(id), dto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除任务' })
  async deleteMission(@Param('id') id: string): Promise<void> {
    return await this.missionService.delete(Number(id));
  }

  @Post('record')
  @ApiTenantHeader()
  @ApiOperation({ summary: '记录任务' })
  @ApiBody({ type: MissionRecordDto })
  async record(@Body() dto: MissionRecordDto, @AuthUser() user: IAuthUser) {
    this.missionService.recordMissionFinish(user?.uid, dto);
  }
}
